package cn.itsource.pay.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.pay.domain.OrderAddress;
import cn.itsource.pay.domain.OrderAdopt;
import cn.itsource.pay.domain.UserAddress;
import cn.itsource.pay.domain.dto.PayDto;
import cn.itsource.pay.mapper.OrderAddressMapper;
import cn.itsource.pay.mapper.OrderAdoptMapper;
import cn.itsource.pay.mapper.UserAddressMapper;
import cn.itsource.pay.service.AlipayService;
import cn.itsource.pay.service.IOrderAdoptService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;


@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService{

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private AlipayService alipayService;

    /**
     * payDto :addressId  petId
     * @param payDto
     * @return
     */
    @Override
    @Transactional
    public String submitOrder(PayDto payDto) {

        Long addressId = payDto.getAddressId();
        Long petId = payDto.getPetId();
        //1.查询用户地址

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);

        //2.用户地址拷贝到订单地址对象,组装数据
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);
        orderAddress.setCreateTime(new Date());
        orderAddress.setUpdateTime(new Date());

        //3.订单地址入库，拿到id
        orderAddressMapper.insertSelective(orderAddress);

        //4.通过宠物id查询宠物数据（商品）
        Pet pet = petMapper.selectByPrimaryKey(petId);

        //5.拼装订单表的数据
        OrderAdopt orderAdopt = new OrderAdopt();
        String body = "宠物id:" + petId + ",地址id:" + addressId;
        orderAdopt.setDigest(body);
        orderAdopt.setState(0);//待支付
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getSaleprice()));
        String orderSn = new Snowflake().nextIdStr();//学会
        orderAdopt.setOrderSn(orderSn);
        orderAdopt.setLastConfirmTime(new Date());
        orderAdopt.setPetId(petId);
        orderAdopt.setUserId(LoginInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());


        //6.把订单地址的id设置给订单表的外键addressId
        orderAdopt.setAddressId(orderAddress.getId());
        //7.订单入库
        orderAdoptMapper.insertSelective(orderAdopt);

        //8.跳转到支付宝，调用我们拷贝过来的代码

        // public String createPayUrl(Double total_amount, String body) {
        String payUrl = alipayService.createPayUrl(pet.getSaleprice(), body,orderSn);
        return payUrl;
    }
}
